package com.qkl.xiatang.app.dto.pond;

import com.qkl.xiatang.app.dto.feed.FeedVO;
import com.qkl.xiatang.app.dto.message.FeedMsgDTO;
import com.qkl.xiatang.app.entity.pond.PondEntity;
import com.qkl.xiatang.app.utils.AppMsgUtils;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.Constants;
import com.qkl.xiatang.app.utils.MsgUtils;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

@Data
public class QueryPondNamesOutDTO {

    private String tableName;

    private String userIdFieldName;

    private String userId;

    private String companyId;

    private Date allocDate;

    private Integer serialNo;

    private String userName;

    private String feedId;

    private Set<String> feedIdSet;

    private String pondId;

    private Set<String> pondIdSet;

    private List<String> pondNames;

    private List<PondEntity> pondList;

    public void parseFeedIdList(){
        if(ObjectUtils.isNotEmpty(feedIdSet)){
            Set<String> set = new TreeSet<>();
            for(String feedIds : feedIdSet ){
                set.addAll(AppUtils.stringToList(feedIds));
            }
            feedIdSet = set;
        }
    }

    public String toMessage(String useMemo){
        return toMessage(useMemo,userName);
    }

    public String toMessage(String useMemo,String titleName){
        String line = Constants.ENTER_LINE;
        String result = "";
        if(ObjectUtils.isNotEmpty(pondNames)){
            result = "[" + StringUtils.trimToEmpty(titleName) + "]" + useMemo + line + AppMsgUtils.pondNamesMessage(pondNames);
        }
        return  result;
    }

    public static String toMessage(List<QueryPondNamesOutDTO> pondNamesList){
        return toMessage(pondNamesList,"");
    }

    public static String toMessage(List<QueryPondNamesOutDTO> pondNamesList,String useMemo){
        return toMessage(pondNamesList,useMemo,null);
    }
    public static String toMessage(List<QueryPondNamesOutDTO> pondNamesList,FeedVO feedVO){
        return toMessage(pondNamesList,"",feedVO);
    }
    public static String toMessage(List<QueryPondNamesOutDTO> pondNamesList,String useMemo,FeedVO feedVO){
        String result = "";
        String s = ",";
        String line = Constants.ENTER_LINE;
        if(ObjectUtils.isNotEmpty(pondNamesList)){
            for(int i = 0,size = pondNamesList.size(); i < size ;i++ ){
                if(i == size -1){
                    s = "";
                    line = "";
                }
                String message = "";
                QueryPondNamesOutDTO namesOutDTO = pondNamesList.get(i);
                if( null == feedVO ){
                    message = namesOutDTO.toMessage(useMemo);
                }else {
                    List<String> feedNames = new ArrayList<>();
                    List<FeedMsgDTO> feedMsgList = feedVO.getFeedMsgList();
                    if(ObjectUtils.isNotEmpty(feedMsgList)){
                        for(FeedMsgDTO msgDTO : feedMsgList){
                            if(namesOutDTO.getFeedIdSet().contains(msgDTO.getFeedId())){
                                feedNames.add(msgDTO.getFeedName());
                            }
                        }
                    }
                    message = namesOutDTO.toMessage(useMemo, String.join(",",feedNames));
                }

                if(StringUtils.isNotEmpty(message)) {
                    result = result + message + s + line;
                }
            }
        }
        return  result;
    }



}
